package com.cluster.platform.service.impl;

import com.cluster.platform.common.api.CommonPage;
import com.cluster.platform.common.api.CommonResult;
import com.cluster.platform.mapper.UmsFocusInfoMapper;
import com.cluster.platform.model.UmsFocusInfo;
import com.cluster.platform.service.UmsFocusInfoService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: UmsFocusInfoServiceImpl
 */
@Service
public class UmsFocusInfoServiceImpl implements UmsFocusInfoService {

    @Autowired
    private UmsFocusInfoMapper umsFocusInfoMapper;

    @Value("${focus.ceiling}")
    private Integer ceiling;

    /**
     * 新增关注
     *
     * @param umsFocusInfo
     */
    @Override
    @Transactional
    public CommonResult<Object> addFocus(UmsFocusInfo umsFocusInfo) {
        if (null == umsFocusInfo || StringUtils.isBlank(umsFocusInfo.getFocusId())) {
            return CommonResult.failed("未获取参数信息");
        }
        Integer focusNumber = searchFocusNumber(umsFocusInfo.getFocusId());
        if (focusNumber > ceiling) {
            return CommonResult.failed("以达到关注上限，请取消一些关注");
        }
        int insertSelective = umsFocusInfoMapper.insertSelective(umsFocusInfo);
        if (insertSelective < 0) {
            return CommonResult.failed("新增失败");
        }
        return CommonResult.success(umsFocusInfo);
    }

    /**
     * 取消关注
     *
     * @param id
     */
    @Override
    @Transactional
    public CommonResult<Object> cancelFocus(String id) {
        if (StringUtils.isBlank(id)) {
            return CommonResult.failed("未获取参数信息");
        }
        UmsFocusInfo umsFocusInfo = new UmsFocusInfo();
        umsFocusInfo.setId(id);
        umsFocusInfo.setUpdateDate(new Date());
        umsFocusInfo.setStatus(0);
        int update = umsFocusInfoMapper.updateByPrimaryKeySelective(umsFocusInfo);
        if (update<0) {
            return CommonResult.failed("更新失败");
        }
        return CommonResult.success(umsFocusInfo);
    }

    /**
     * 查询关注信息
     *
     * @param umsFocusInfo
     */
    @Override
    public CommonResult<Object> searchFocusList(UmsFocusInfo umsFocusInfo) {
        List<UmsFocusInfo> umsFocusInfos = umsFocusInfoMapper.searchFocusList(umsFocusInfo);
        if (null == umsFocusInfos || umsFocusInfos.isEmpty()) {
            return CommonResult.failed("未查询到信息");
        }
        return CommonResult.success(umsFocusInfos);
    }

    /**
     * 分页查询
     *
     * @param umsFocusInfo
     * @param pageNum
     * @param pageSize
     */
    @Override
    public CommonResult<Object> searchFocusPage(UmsFocusInfo umsFocusInfo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UmsFocusInfo> umsFocusInfos = umsFocusInfoMapper.searchFocusList(umsFocusInfo);
        if (null == umsFocusInfos || umsFocusInfos.isEmpty()) {
            return CommonResult.failed("未查询到数据");
        }
        return CommonResult.success(CommonPage.restPage(umsFocusInfos));
    }

    /**
     * 查询已关注人数
     *
     * @param focusId
     */
    @Override
    public Integer searchFocusNumber(String focusId) {
        UmsFocusInfo umsFocusInfo = new UmsFocusInfo();
        umsFocusInfo.setFocusId(focusId);
        List<UmsFocusInfo> umsFocusInfos = umsFocusInfoMapper.searchFocusList(umsFocusInfo);
        if (null == umsFocusInfos || umsFocusInfos.isEmpty()) {
            return 0;
        }
        return umsFocusInfos.size();
    }

    /**
     * 查询已被多少人关注
     *
     * @param userId
     */
    @Override
    public Integer searchBeFocusNumber(String userId) {
        UmsFocusInfo umsFocusInfo = new UmsFocusInfo();
        umsFocusInfo.setUserId(userId);
        List<UmsFocusInfo> umsFocusInfos = umsFocusInfoMapper.searchFocusList(umsFocusInfo);
        if (null == umsFocusInfos || umsFocusInfos.isEmpty()) {
            return 0;
        }
        return umsFocusInfos.size();
    }
}
